package com.example.spring_start.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.exception.BusinessException;
import com.example.exception.BusinessExceptionCode;
import com.example.pojo.banner;
import com.example.req.UserReq;
import com.example.req.bannerReq;
import com.example.resp.PageResp;
import com.example.spring_start.mapper.UserMapper;
import com.example.pojo.User;
import com.example.req.LoginUserReq;
import com.example.req.LoginUserSaveReq;
import com.example.resp.LoginUserResp;
import com.example.utils.CopyUtil;
import com.example.utils.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private SnowFlake snowFlake;

    @Override
    public void register(LoginUserSaveReq req){
        User user = CopyUtil.copy(req, User.class);
        if(req.getUserid()==0) {
            User userDb = selectByUserName(req.getUsername());
            if (ObjectUtils.isEmpty(userDb)) {
                user.setUserid(snowFlake.nextId());
                userMapper.insert(user);
            } else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        }else {
            user.setUsername(null);
            user.setUserpassword(null);
            user.setUseridentity(0);
            userMapper.updateById(user);
        }
    }

    @Override
    public void saveUser(LoginUserSaveReq loginUserSaveReq){
        User user = CopyUtil.copy(loginUserSaveReq, User.class);
        if(loginUserSaveReq.getUserid()==0) {
            User userDb = selectByUserName(loginUserSaveReq.getUsername());
            if (ObjectUtils.isEmpty(userDb)) {
                user.setUserid(snowFlake.nextId());
                userMapper.insert(user);
            } else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        }else {
            user.setUsername(null);
            user.setUserpassword(null);
            userMapper.updateById(user);
        }
    }

    @Override
    public LoginUserResp login(LoginUserReq req){
        User userDb = selectByUserName(req.getUsername());
        if(ObjectUtils.isEmpty(userDb)){
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if(userDb.getUserpassword().equals(req.getUserpassword())){
                LoginUserResp loginUserResp = CopyUtil.copy(userDb, LoginUserResp.class);
                return loginUserResp;
            }else {
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }

    public User selectByUserName(String username){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUsername,username);
        List<User> userEntityList = userMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(userEntityList)){
            return null;
        }else {
            return userEntityList.get(0);
        }
    }

    @Override
    public PageResp<User> getUserList(UserReq userReq){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(userReq.getUsername())){
            queryWrapper.lambda().eq(User::getUsername,userReq.getUsername());
        }
        if (!ObjectUtils.isEmpty(userReq.getUsername())){
            queryWrapper.lambda().eq(User::getUsername,userReq.getUsername());
        }
        Page<User> page = new Page<>(userReq.getPage(),userReq.getSize());
        IPage<User> userIPage = userMapper.selectPage(page,queryWrapper);
        PageResp<User> pageResp = new PageResp<>();
        pageResp.setTotal(userIPage.getTotal());
        pageResp.setList(userIPage.getRecords());
        return pageResp;
    }
}
